#include "elk_download_item.h"
#include "ui_elk_download_item.h"
#include <QMetaEnum>

ElkDownloadItem::ElkDownloadItem(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::ElkDownloadItem)
{
    ui->setupUi(this);

    ui->pushButtonPause->setVisible(false);
    ui->pushButtonResume->setVisible(false);
    ui->pushButtonCancel->setVisible(false);
    ui->pushButtonRetry->setVisible(false);
}

ElkDownloadItem::~ElkDownloadItem()
{
    delete ui;
}

void ElkDownloadItem::attach(ElkDownloadTask *task)
{
    if (task_) {
        disconnect(task_, nullptr, this, nullptr);
        task_->deleteLater();
    }

    task_ = task;
    connect(task_, SIGNAL(finished()), this, SLOT(finished()));
    connect(task_, SIGNAL(interrupted()), this, SLOT(interrupted()));
    connect(task_, SIGNAL(downloadProgress(qint64, qint64, qint64)),
            this, SLOT(downloadProgress(qint64, qint64, qint64)));

    connect(task_, SIGNAL(urlChanged(const QString &)),
            this, SLOT(urlChanged(const QString &)));
    connect(task_, SIGNAL(fileError(QFile::FileError, const QString &)),
            this, SLOT(fileError(QFile::FileError, const QString &)));
    connect(task_, SIGNAL(replyError(QNetworkReply::NetworkError, const QString &)),
            this, SLOT(replyError(QNetworkReply::NetworkError, const QString &)));

    resetDisply();
}

void ElkDownloadItem::finished()
{
    ui->labelProgressStatus->setText(tr("Complete"));

    ui->pushButtonPause->setVisible(false);
    ui->pushButtonResume->setVisible(false);
    ui->pushButtonCancel->setVisible(false);
    ui->pushButtonRetry->setVisible(false);
}

void ElkDownloadItem::interrupted()
{
    ui->labelProgressStatus->setText(tr("Interrupted"));

    ui->pushButtonPause->setVisible(false);
    ui->pushButtonResume->setVisible(false);
    ui->pushButtonCancel->setVisible(true);
    ui->pushButtonRetry->setVisible(true);
}

void ElkDownloadItem::downloadProgress(qint64 bytesReceived, qint64 bytesTotal, qint64 bytesFileSize)
{
    QString text;
    text += QString("%1/%2").arg(bytesReceived).arg(bytesTotal);
    text += QString("(%1)").arg(bytesFileSize);
    ui->labelProgress->setText(text);
}

void ElkDownloadItem::urlChanged(const QString &url)
{
    ui->labelUrl->setText(url);
}

void ElkDownloadItem::fileError(QFileDevice::FileError, const QString &errorString)
{
    ui->labelProgressStatus->setText(errorString);

    ui->pushButtonPause->setVisible(false);
    ui->pushButtonResume->setVisible(false);
    ui->pushButtonCancel->setVisible(true);
    ui->pushButtonRetry->setVisible(false);
}

void ElkDownloadItem::replyError(QNetworkReply::NetworkError error, const QString &errorString)
{
    QMetaEnum errorEnum = QMetaEnum::fromType<QNetworkReply::NetworkError>();
    ui->labelProgressStatus->setText(errorEnum.valueToKey(error) + QString(" : ") + errorString);

    ui->pushButtonPause->setVisible(false);
    ui->pushButtonResume->setVisible(false);
    ui->pushButtonCancel->setVisible(true);
    ui->pushButtonRetry->setVisible(true);
}

void ElkDownloadItem::on_pushButtonPause_clicked()
{
    task_->abort();
    ui->labelProgressStatus->setText(tr("Paused"));

    ui->pushButtonPause->setVisible(false);
    ui->pushButtonResume->setVisible(true);
    ui->pushButtonCancel->setVisible(true);
    ui->pushButtonRetry->setVisible(false);
}

void ElkDownloadItem::on_pushButtonResume_clicked()
{
    task_->retry();
    resetDisply();
}

void ElkDownloadItem::on_pushButtonCancel_clicked()
{
    task_->clear();
    ui->labelProgressStatus->setText(tr("Canceled"));

    ui->pushButtonPause->setVisible(false);
    ui->pushButtonResume->setVisible(false);
    ui->pushButtonCancel->setVisible(false);
    ui->pushButtonRetry->setVisible(true);
}

void ElkDownloadItem::on_pushButtonRetry_clicked()
{
    task_->retry();
    resetDisply();
}

void ElkDownloadItem::on_pushButtonDelete_clicked()
{
    task_->clear();
    task_->deleteLater();
    this->deleteLater();
}

void ElkDownloadItem::resetDisply()
{
    ui->labelFileName->setText(task_->fileName());
    ui->labelUrl->setText(task_->url());

    ui->labelProgress->setText(QString());
    ui->labelProgressStatus->setText(QString());

    ui->pushButtonPause->setVisible(true);
    ui->pushButtonResume->setVisible(false);
    ui->pushButtonCancel->setVisible(true);
    ui->pushButtonRetry->setVisible(false);
}
